package com.example.demo.one;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * @author chensixiang chensixiang1234@gmail.com
 */
public class Producer {
    public static final String QUEUE_NAME = "mirror_hello";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 创建工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.255.133");
        factory.setUsername("admin");
        factory.setPassword("123");
        // 获取连接
        Connection conn = factory.newConnection();
        // 创建信道
        Channel channel = conn.createChannel();
        // 生成队列
        boolean durable = true; // 队列持久化
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-max-priority", 10);// 0-255
        channel.queueDeclare(QUEUE_NAME, durable, false, false, arguments);

        for (int i = 1; i < 11; i++) {
            String msg = "info" + i;
            if (i == 5) {
                AMQP.BasicProperties properties = new AMQP.BasicProperties().builder().priority(5).build();
                channel.basicPublish("", QUEUE_NAME, properties, msg.getBytes());
            } else {
                channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
            }
        }
        String message = "Hello Word";
        // 消息持久化
        // channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
        System.out.println("消息发送完毕");
    }
}
